﻿<!--
Filename: SilverTextEditor.xaml
Version: 20140921
-->

<!-- Note: if the XAML doesn't compile with error about an Easing function, then remove WPFToolkit and xceed.Wpf.Toolkit from project References -->

<UserControl x:Class="SilverTextEditor.SilverTextEditor"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d:DesignHeight="240" d:DesignWidth="593"
  xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:btn="clr-namespace:ImageButtons;assembly=ImageButtons"
  FontFamily="Calibri"
  BorderThickness="0"
  >

  <UserControl.Resources>
    <ResourceDictionary>

      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/SilverTextEditor;component/Themes/Generic.xaml" />
      </ResourceDictionary.MergedDictionaries>

      <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

    </ResourceDictionary>
  </UserControl.Resources>

  <Grid
    x:Name="LayoutRoot"
    DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
    >
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="DragStates">
        <VisualStateGroup.Transitions>
          <VisualTransition GeneratedDuration="0:0:0.3">
            <VisualTransition.GeneratedEasingFunction>
              <CircleEase EasingMode="EaseIn"/>
            </VisualTransition.GeneratedEasingFunction>
          </VisualTransition>
        </VisualStateGroup.Transitions>
        <VisualState x:Name="Normal"/>
        <VisualState x:Name="DragOver">
          <Storyboard>
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)" Storyboard.TargetName="rtb">
              <EasingColorKeyFrame KeyTime="0" Value="#FFFFF7D1"/>
            </ColorAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
      </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <!-- TODO: add ContentZoom option to work like Word? -->

    <Grid Name="MainGrid">

      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>

      <!-- Toolbar -->
      <toolkit:WrapPanel x:Name="Toolbar"
                    Orientation="Horizontal"
                    Grid.Row="0"
                    Background="{StaticResource ToolbarBackground}"
                    >
        <toolkit:WrapPanel.Resources>
          <Style TargetType="StackPanel">
            <Setter Property="Margin" Value="2,0" />
          </Style>
          <Style TargetType="ComboBox">
            <Setter Property="Margin" Value="1,5" />
          </Style>
          <Style TargetType="btn:ImageButton">
            <Setter Property="Margin" Value="1,5" />
          </Style>
          <Style TargetType="btn:ImageToggleButton">
            <Setter Property="Margin" Value="1,5" />
          </Style>
          <Style TargetType="ToggleButton">
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Margin" Value="2,5" />
            <Setter Property="Width" Value="20" />
            <Setter Property="Height" Value="20" />
          </Style>
        </toolkit:WrapPanel.Resources>

        <!-- File Toolbar -->
        <StackPanel Orientation="Horizontal">

          <btn:ImageButton
              x:Name="btnOpen" Click="btnOpen_Click"
              ToolTipService.ToolTip="{Binding tooltip_Open, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Import.png"
              ImageHover="Images/Import_hover.png"
              />

          <btn:ImageButton
              x:Name="btnSave" Click="btnSave_Click"
              ToolTipService.ToolTip="{Binding tooltip_Save, Source={StaticResource localizedStrings}}"
              Image="Images/Export.png"
              ImageHover="Images/Export_hover.png"
              />

          <btn:ImageButton
              x:Name="btnClear" Click="btnClear_Click"
              ToolTipService.ToolTip="{Binding tooltip_Clear, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Clear.png"
              ImageHover="Images/Clear_hover.png"
              />

          <btn:ImageButton
              x:Name="btnPrint"
              Click="btnPrint_Click"
              ToolTipService.ToolTip="{Binding tooltip_Print, Source={StaticResource localizedStrings}}"
              Image="Images/Print.png"
              ImageHover="Images/Print_hover.png"
              />

        </StackPanel>

        <!--Clipboard Toolbar -->
        <StackPanel Orientation="Horizontal">

          <btn:ImageButton
              x:Name="btnCut" Click="btnCut_Click"
              ToolTipService.ToolTip="{Binding tooltip_Cut, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Cut.png"
              ImageHover="Images/Cut_hover.png"
              />

          <btn:ImageButton
              x:Name="btnCopy" Click="btnCopy_Click"
              ToolTipService.ToolTip="{Binding tooltip_Copy, Source={StaticResource localizedStrings}}"
              Image="Images/Copy.png"
              ImageHover="Images/Copy_hover.png"
              />

          <btn:ImageButton
              x:Name="btnPaste" Click="btnPaste_Click"
              ToolTipService.ToolTip="{Binding tooltip_Paste, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Paste.png"
              ImageHover="Images/Paste_hover.png"
              />

        </StackPanel>

        <!--Fonts Toolbar-->
        <StackPanel Orientation="Horizontal">

          <ComboBox x:Name="cmbFonts"
                      Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
                      FontFamily="{Binding SelectedItem.FontFamily, RelativeSource={RelativeSource Self}}"
                      FontSize="{Binding SelectedItem.FontSize, RelativeSource={RelativeSource Self}}"
                      SelectionChanged="cmbFonts_SelectionChanged"
                      IsDropDownOpen="False"
                      >
            <ComboBoxItem Content="Arial" Tag="Arial" FontFamily="Arial" FontSize="12"/>
            <ComboBoxItem Content="Arial Black" Tag="Arial Black" FontFamily="Arial Black" FontSize="12"/>
            <ComboBoxItem Content="Calibri" Tag="Calibri" FontFamily="Calibri" IsSelected="True" FontSize="14"/>
            <ComboBoxItem Content="Comic Sans MS" Tag="Comic Sans MS" FontFamily="Comic Sans MS" FontSize="12"/>
            <ComboBoxItem Content="Courier New" Tag="Courier New" FontFamily="Courier New" FontSize="12"/>
            <ComboBoxItem Content="Georgia" Tag="Georgia" FontFamily="Georgia" FontSize="12"/>
            <ComboBoxItem Content="Lucida Sans Unicode" Tag="Lucida Sans Unicode" FontFamily="Lucida Sans Unicode" FontSize="12"/>
            <ComboBoxItem Content="Portable User Interface" Tag="Portable User Interface" FontFamily="Portable User Interface" FontSize="12"/>
            <ComboBoxItem Content="Times New Roman" Tag="Times New Roman" FontFamily="Times New Roman" FontSize="12"/>
            <ComboBoxItem Content="Trebuchet MS" Tag="Trebuchet MS" FontFamily="Trebuchet MS" FontSize="12"/>
            <ComboBoxItem Content="Verdana" Tag="Verdana" FontFamily="Verdana" FontSize="12"/>
            <ComboBoxItem Content="Webdings" Tag="Webdings" FontSize="12"/>
          </ComboBox>

          <ComboBox x:Name="cmbFontSizes"
                      Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
                      FontSize="14"
                      SelectionChanged="cmbFontSizes_SelectionChanged"
                      IsDropDownOpen="False"
                      >
            <ComboBoxItem Content="8" Tag="8"/>
            <ComboBoxItem Content="9" Tag="9"/>
            <ComboBoxItem Content="10" Tag="10"/>
            <ComboBoxItem Content="11" Tag="11"/>
            <ComboBoxItem Content="12" Tag="12"/>
            <ComboBoxItem Content="14" Tag="14"/>
            <ComboBoxItem Content="16" Tag="16" IsSelected="True"/>
            <ComboBoxItem Content="18" Tag="18"/>
            <ComboBoxItem Content="20" Tag="20"/>
            <ComboBoxItem Content="22" Tag="22"/>
            <ComboBoxItem Content="24" Tag="24"/>
            <ComboBoxItem Content="26" Tag="26"/>
            <ComboBoxItem Content="28" Tag="28"/>
            <ComboBoxItem Content="36" Tag="36"/>
            <ComboBoxItem Content="48" Tag="48"/>
            <ComboBoxItem Content="72" Tag="72"/>
          </ComboBox>

          <ComboBox x:Name="cmbFontColors"
                      Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
                      SelectionChanged="cmbFontColors_SelectionChanged"
                      SelectedIndex="0"
                      IsDropDownOpen="False"
                      >
            <ComboBoxItem Tag="FF0A0200" IsSelected="True">
              <Rectangle Width="22" Height="14" Fill="#0A0200" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFADADAD">
              <Rectangle Width="22" Height="14" Fill="#ADADAD" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFE32F21">
              <Rectangle Width="22" Height="14" Fill="#E32F21" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFEA8934">
              <Rectangle Width="22" Height="14" Fill="#EA8934" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FF67847D">
              <Rectangle Width="22" Height="14" Fill="#67847D" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FF0057AE">
              <Rectangle Width="22" Height="14" Fill="#0057AE" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FF0F75E3">
              <Rectangle Width="22" Height="14" Fill="#0F75E3" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFB0B535">
              <Rectangle Width="22" Height="14" Fill="#B0B535" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FF582770">
              <Rectangle Width="22" Height="14" Fill="#582770" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFC11F3E">
              <Rectangle Width="22" Height="14" Fill="#C11F3E" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFFC9D9A">
              <Rectangle Width="22" Height="14" Fill="#FC9D9A" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFFFFFFF">
              <Rectangle Width="22" Height="14" Fill="#FFFFFF" Stroke="Black" StrokeThickness="1" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FF0D4161">
              <Rectangle Width="22" Height="14" Fill="#0D4161" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FFF9D423">
              <Rectangle Width="22" Height="14" Fill="#F9D423" />
            </ComboBoxItem>
            <ComboBoxItem Tag="FF3FB8AF">
              <Rectangle Width="22" Height="14" Fill="#3FB8AF" />
            </ComboBoxItem>
          </ComboBox>

          <btn:ImageButton
              x:Name="btnBold"
              Click="btnBold_Click"
              ToolTipService.ToolTip="{Binding tooltip_Bold, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Bold.png"
              ImageHover="Images/Bold_hover.png"
              />

          <btn:ImageButton
              x:Name="btnItalic" Click="btnItalic_Click"
              ToolTipService.ToolTip="{Binding tooltip_Italic, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Italic.png"
              ImageHover="Images/Italic_hover.png"
              />

          <btn:ImageButton
              x:Name="btnUnderline" Click="btnUnderline_Click"
              ToolTipService.ToolTip="{Binding tooltip_Underline, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Underline.png"
              ImageHover="Images/Underline_hover.png"
            />

        </StackPanel>

        <!-- Insert Toolbar -->
        <StackPanel Orientation="Horizontal">

          <!--
            <btn:ImageButton
              x:Name="btnImage" Click="btnImage_Click"
              ToolTipService.ToolTip="{Binding tooltip_Image, Source={StaticResource localizedStrings}}"
              Image="Images/Image.png"
              ImageHover="Images/Image_hover.png"
              />
            />
            -->

          <btn:ImageButton
              x:Name="btnHyperlink" Click="btnHyperlink_Click"
              ToolTipService.ToolTip="{Binding tooltip_Hyperlink, Source={StaticResource localizedStrings}}"
              Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Image="Images/Hyperlink.png"
              ImageHover="Images/Hyperlink_hover.png"
              />

          <btn:ImageButton
              x:Name="btnTimestamp" Click="btnTimestamp_Click"
              ToolTipService.ToolTip="{Binding tooltip_Timestamp, Source={StaticResource localizedStrings}}"
              Visibility="Collapsed"
              Image="Images/InsertTime.png"
              ImageHover="Images/InsertTime_hover.png"
              />
          <!-- Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}" -->

        </StackPanel>

        <!-- Display Toolbar -->
        <StackPanel Orientation="Horizontal">

          <btn:ImageToggleButton
              x:Name="btnRTL"
              IsChecked="{Binding RTL, Mode=TwoWay}"
              ToolTipService.ToolTip="{Binding tooltip_TextDirection, Source={StaticResource localizedStrings}}"
              Image="Images/LTR.png"
              ImageHover="Images/LTR_hover.png"
              ImageChecked="Images/RTL.png"
              ImageCheckedHover="Images/RTL_hover.png"
              />

          <btn:ImageToggleButton
              x:Name="btnHighlight"
              Visibility="Collapsed"
              Checked="btnHighlight_Checked"
              ToolTipService.ToolTip="{Binding txt_Highlight, Source={StaticResource localizedStrings}}"
              Image="Images/HighlightNone.png"
              ImageHover="Images/HighlightNone_hover.png"
              ImageChecked="Images/Highlight.png"
              ImageCheckedHover="Images/Highlight_hover.png"
              />

          <btn:ImageToggleButtonSideBySide
              x:Name="btnEditable"
              IsChecked="{Binding Editable, Mode=TwoWay}"
              ToolTipService.ToolTip="{Binding tooltip_EditViewMode, Source={StaticResource localizedStrings}}"
              Image="Images/ReadingMode.png"
              ImageHover="Images/ReadingMode_hover.png"
              ImageChecked="Images/EditMode.png"
              ImageCheckedHover="Images/EditMode_hover.png"
              ImageSelector="Images/Selector.png"
              />

          <!-- -->
          <ToggleButton
            x:Name="btnMarkUp"
            Checked="btnMarkUp_Checked"
            Unchecked="btnMarkUp_Checked"
            ToolTipService.ToolTip="{Binding txt_XAML, Source={StaticResource localizedStrings}}"
            Visibility="Collapsed"
            >
            <TextBlock FontSize="14" FontWeight="Bold" Text="&lt;&gt;"/>
          </ToggleButton>
          <!-- Visibility="{Binding Editable, Converter={StaticResource BooleanToVisibilityConverter}}" -->

        </StackPanel>

      </toolkit:WrapPanel>

      <Grid Name="RTBGrid" Grid.Row="1">

        <!-- //Note: this is the correct way to cast a shadow, to use a Grid and overlapping controls, not use a Border arround the control with effect, since the content in it won't be h/w accelerated then (can use a Border or Rectangle etc. under the h/w accelerated control instead)
          <Rectangle Fill="White" Margin="0,1,1,0">
            <Rectangle.Effect>
              <DropShadowEffect ShadowDepth="1" Direction="371" BlurRadius="7" Opacity="0.345"/>
            </Rectangle.Effect>
          </Rectangle>
          -->

        <RichTextBox
          x:Name="rtb"
          Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
          AllowDrop="True"
          BorderThickness="0"
          Style="{StaticResource RichTextBoxStyle1}"
          HorizontalScrollBarVisibility="Auto"
          VerticalScrollBarVisibility="Auto"
          FontSize="20"

          MouseLeftButtonDown="rtb_MouseLeftButtonDown"
          MouseRightButtonDown="rtb_MouseRightButtonDown" MouseRightButtonUp="rtb_MouseRightButtonUp"
          MouseMove="rtb_MouseMove"
          Drop="rtb_Drop"
          DragEnter="rtb_DragEnter" DragLeave="rtb_DragLeave"
          >
          <!-- TextWrapping="Wrap"     *** NO SUCH PROPERTY IN WPF RichTextBox, WRAPPING BY DEFAULT (see: http://stackoverflow.com/questions/1368047/c-wpf-disable-text-wrap-of-richtextbox) *** -->
          <!-- Margin="8,10,8,10" -->

          <!-- ****************** -->
          <!-- TODO: following block is designer data only (can comment out) - SilverTextEditor_Loaded method that handles Loaded event loads in ActivityDescription.sav XAML file -->
          <!--
              <Paragraph FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
                <Run FontSize="24" FontFamily="Portable User Interface" Foreground="#FF008000" FontWeight="Bold" FontStyle="Italic" FontStretch="Normal" Text="Lorem ipsum" />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text=" dolor sit amet, consectetur adipiscing elit. Integer mattis ligula in neque semper bibendum. " />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Bold" FontStyle="Normal" FontStretch="Normal" Text="Morbi imperdiet" />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text=", neque et consectetur sollicitudin, sem arcu egestas diam, sit amet consequat odio velit id tortor. " />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Bold" FontStyle="Normal" FontStretch="Normal" Text="Quisque congue nisl sed nunc sagittis dictum. Vivamus condimentum urna lacus." />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text=" " />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF0000FF" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text="Suspendisse" />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text=" porttitor pretium quam ut accumsan. Curabitur vulputate, felis non fringilla vehicula, turpis neque vulputate lectus, " />
                <Run FontSize="22" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Italic" FontStretch="Normal" Text="id faucibus turpis ante quis urna. Quisque sem tellus, sodales a interdum in, lacinia ut quam." />
              </Paragraph>
              <Paragraph FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left" />
              <Paragraph FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left" >
                <Run FontSize="26" FontFamily="Georgia" Foreground="#FF0000FF" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text="Praesent sit amet odio ipsum," />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text=" non fermentum nunc. Aliquam erat volutpat. Morbi sit amet enim risus. Maecenas eget odio ut massa adipiscing feugiat quis eget enim. " />
                <Run FontSize="18" FontFamily="Portable User Interface" Foreground="#FFFF0000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text="Nulla facilisi." />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text=" Phasellus condimentum, metus vel congue molestie, mi felis tempus tortor, id aliquam turpis elit vitae lorem. " />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text="Morbi ut eros" />
                <Run FontSize="20" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Text=" in lectus vehicula euismod." />
              </Paragraph>
            -->
          <!-- ****************** -->

        </RichTextBox>

        <Canvas x:Name="highlightCanvas" IsHitTestVisible="False" />
        <!-- Margin="8,10,8,10" -->

        <TextBox x:Name="xamlTb"
                   Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                   BorderThickness="0"
                   TextWrapping="Wrap"
                   HorizontalScrollBarVisibility="Auto"
                   VerticalScrollBarVisibility="Auto"
                   FontSize="20"
                   FontFamily="Lucida Console"
                   Visibility="Collapsed"
                   IsTabStop="False"
                   />
      </Grid>

    </Grid>

  </Grid>

</UserControl>
